import pygtk
pygtk.require('2.0')
import gtk
import os

from mokocard.util.sqlutil import SqlUtil
from mokocard.gui.modaldialog import ModalDialog

class ArrangePage(gtk.VBox):
    
    idx = -1

    def active(self, counter):

        # clean entry
        self.entryUQuestion.set_text("")
        self.entryUAnswer.set_text("")
        
        if self.sqlUtil == None:
            modal = ModalDialog("No database open")
            return None
                
        if counter >= 0 and self.sqlUtil.cards != None:
            
            self.idx = counter
            self.entryUQuestion.set_text(self.sqlUtil.cards[counter].question)
            self.entryUAnswer.set_text(self.sqlUtil.cards[counter].answer)

        
    def changeRow(self, widget):
        None
        
        if self.sqlUtil == None:
            modal = ModalDialog("No database open")
            return None
            
        if self.entryUQuestion.get_text() == None or self.entryUAnswer.get_text() == None or self.entryUQuestion.get_text().strip() == "" or self.entryUAnswer.get_text().strip() == "":
            modal = ModalDialog("No question or answer")
            return None
        
        ret = self.sqlUtil.changeCard(self.entryUQuestion.get_text() , self.entryUAnswer.get_text(), self.idx)

        if ret == True:
            modal = ModalDialog("Record Updated")
        else:
            modal = ModalDialog(ret)
            
            
    def insertRow(self, widget):
        
        if self.sqlUtil == None:
            modal = ModalDialog("No database open")
            return None
        
        if self.entryIQuestion.get_text() == None or self.entryIAnswer.get_text() == None or self.entryIQuestion.get_text().strip() == "" or self.entryIAnswer.get_text().strip() == "":
            modal = ModalDialog("No question or answer")
            return None

        ret = self.sqlUtil.insertCard(self.entryIQuestion.get_text() , self.entryIAnswer.get_text())
        
        if ret == True:
            modal = ModalDialog("Record Inserted")
            self.entryIAnswer.set_text("")
            self.entryIQuestion.set_text("")
        else:
            modal = ModalDialog(ret)
    
    def build(self, configData):
        
        # begin insert data zone
        
        # build table
        tableInsertData = gtk.Table(4,1,False)
        tableInsertData.set_border_width(10)
        tableInsertData.show()
        
        # parameter homogeneous, spacing
        hEntryIQuestion = gtk.HBox(False,10)
        hEntryIQuestion.show()
        
        self.entryIQuestion = gtk.Entry()
        self.entryIQuestion.show()
        
        lblIQuestion = gtk.Label("Question: ")
        lblIQuestion.show()
        
        # parameters child, expand, fill, padding
        hEntryIQuestion.pack_start(lblIQuestion, False, False, 10)
        hEntryIQuestion.pack_start(self.entryIQuestion, True, True, 10)
        
        # parameter homogeneous, spacing
        hEntryIAnswer = gtk.HBox(False,10)
        hEntryIAnswer.show()
        
        self.entryIAnswer = gtk.Entry()
        self.entryIAnswer.show()
        
        lblIAnswer = gtk.Label("Answer: ")
        lblIAnswer.show()
        
        hEntryIAnswer.pack_start(lblIAnswer, False, False, 10)
        hEntryIAnswer.pack_start(self.entryIAnswer, True, True, 10)
        
        btInsertRow = gtk.Button("Insert")
        btInsertRow.connect("clicked", self.insertRow)
        btInsertRow.set_size_request(configData.getButtonArrangeWidth(), configData.getButtonArrangeHeight())
        btInsertRow.show()
        
        # end insert data zone
        lblInsertRow = gtk.Label("Insert Row ")
        lblInsertRow.show()
        
        tableInsertData.attach(lblInsertRow,0,1,0,1, ypadding=10)
        tableInsertData.attach(hEntryIQuestion,0,1,1,2, ypadding=10)
        tableInsertData.attach(hEntryIAnswer,0,1,2,3, ypadding=10)
        tableInsertData.attach(btInsertRow,0,1,3,4, ypadding=10)
        
        # end insert data zone
        
        # begin update data zone
        
        # build table
        
        tableUpdateData = gtk.Table(4,1,False)
        tableUpdateData.set_border_width(10)
        tableUpdateData.show()
        
        # parameter homogeneous, spacing
        hEntryUQuestion = gtk.HBox(False,10)
        hEntryUQuestion.show()
        
        self.entryUQuestion = gtk.Entry()
        self.entryUQuestion.show()
        
        lblUQuestion = gtk.Label("Question: ")
        lblUQuestion.show()
        
        # parameters child, expand, fill, padding
        hEntryUQuestion.pack_start(lblUQuestion, False, False, 10)
        hEntryUQuestion.pack_start(self.entryUQuestion, True, True, 10)
        
        # parameter homogeneous, spacing
        hEntryUAnswer = gtk.HBox(False,10)
        hEntryUAnswer.show()
        
        self.entryUAnswer = gtk.Entry()
        self.entryUAnswer.show()
        
        lblUAnswer = gtk.Label("Answer: ")
        lblUAnswer.show()
        
        hEntryUAnswer.pack_start(lblUAnswer, False, False, 10)
        hEntryUAnswer.pack_start(self.entryUAnswer, True, True, 10)
        
        btUpdateRow = gtk.Button("Update")
        btUpdateRow.connect("clicked", self.changeRow)
        btUpdateRow.set_size_request(configData.getButtonArrangeWidth(), configData.getButtonArrangeHeight())
        btUpdateRow.show()
        
        # end insert data zone
        lblUpdateRow = gtk.Label("Update Current Row ")
        lblUpdateRow.show()
        
        tableUpdateData.attach(lblUpdateRow,0,1,0,1, ypadding=10)
        tableUpdateData.attach(hEntryUQuestion,0,1,1,2, ypadding=10)
        tableUpdateData.attach(hEntryUAnswer,0,1,2,3, ypadding=10)
        tableUpdateData.attach(btUpdateRow,0,1,3,4, ypadding=10)
        
        # end update data zone
        
        self.pack_start(tableInsertData, False, False, 10)
        self.pack_start(tableUpdateData, False, False, 10)
        
    def __init__ (self, configData, sqlUtil):
        
        self.sqlUtil = sqlUtil
        
        # ten pixels space
        gtk.VBox.__init__(self, False, 10)
        
        self.build(configData)
        self.show()